<?php
require_once "Workflow/AMWorkFlow.php";
require_once "Workflow/MState.php";
require_once "MConfig.php";

class MWorkflow extends AMWorkFlow {

	private $default_source = 'XML/mworkflow.xml';

	public function __construct(){parent::__construct();}

	public function load($source=null){
		if ($source===null) {
			$source = $this->default_source;
		}

		if(MConfig::checkFile($source)):
		MConfig::init($source);
		$wfSource = MConfig::$arCfg;
		
		if($wfSource){
			$this->feed($wfSource['state']);
			$this->initial_state = $wfSource['initial_state'];
			return true;
		}else{
			return false;
		}
		else:
		return false;
		endif;
	}
	private function feed($arWF){
		$stateCol = new MStateCollection();
		
		foreach($arWF as $state_nr => $states){
			//echo "state nr=".$state_nr . ": <br>";
			foreach($states as $state => $transitions){
				if($state == '@attributes'){
					//echo "STATE:<b>".$arWF[$state_nr][$state]['id']."</b><br>";//create
					$state_id = $arWF[$state_nr][$state]['id'];
					$state_title = $arWF[$state_nr][$state]['title']; 
					$stateCol[$state_id] = new MState($state_id, $state_title);
				}else{
					if(is_array($transitions)):
						//echo "Transitions  - ".$state . "= ".print_r($transitions)."<br>";
						foreach($transitions as $transition){
							$stateCol[$state_id]->addTransition(new MTransition($transition));
						}
					else:
						//echo "Transitions  - ".$state . "= $transitions<br>";
						$stateCol[$state_id]->addTransition(new MTransition($transitions));
					endif;
				};
			};
			//echo "<hr>";
		};
		$this->states($stateCol);
		//print_r($stateCol);
		//echo $stateCol['private']->id;
	}
}
?>